package com.gitee.yusugar.pay.alipay.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class AlipayConfig {

    // 商户 appId
    private String appId;
    // 私钥 pkcs8格式的
    private String rsaPrivateKey;
    // 服务器异步通知页面路径
    private String notifyUrl;
    // 页面跳转同步通知页面路径
    private String returnUrl;
    // 请求网关地址
    private String url;
    // 编码
    private String charset = "UTF-8";
    // 返回格式
    private String format = "json";
    // 支付宝公钥
    private String alipayPublicKey;
    // RSA2
    private String signType = "RSA2";

    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private AlipayClient alipayClient;

    /**
     * 构建 AlipayClient 对象
     *
     * @return 对象
     */
    public AlipayClient buildAlipayClient() {
        if ( alipayClient == null ){
            synchronized ( this ){
                if ( alipayClient == null )
                    return alipayClient = new DefaultAlipayClient( url, appId, rsaPrivateKey, format, charset, alipayPublicKey, signType );
            }
        }

        return alipayClient;
    }
}
